package com.threeox.commonlibrary.util.notify;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;

import com.threeox.commonlibrary.CommonApplcation;
import com.threeox.commonlibrary.R;
import com.threeox.commonlibrary.util.ActivityUtils;
import com.threeox.commonlibrary.util.CommonConstant;

/**
 *
 * @ClassName: NotifyUtil
 *
 * @Description: TODO(通知的工具类)
 *
 * @author 赵屈犇
 *
 * @date 创建时间: 2018/5/18 18:51
 *
 * @version 1.0
 *
 */
public class NotifyUtil {

	private CommonApplcation mApplcation;
	private NotificationManager notificationManager;
	private Builder builder;
	private Intent mIntent = null;
	private Class<? extends Intent> mInClass;

	private NotifyUtil() {
		mApplcation = CommonApplcation.getInstance();
	}

	public static NotifyUtil getInstance() {
		return new NotifyUtil();
	}

	public Builder getBuilder() {
		return builder;
	}

	/**
	 * 设置Builder
     *
     * @param channelId
     * 			  通道ID
     * @param channelName
     * 			  通道名称
	 * @param ticker
	 *            在顶部状态栏中的提示信息
	 * @param title
	 *            设置通知中心的标题
	 * @param content
	 *            设置通知中心中的内容
	 * @param sound
	 *            是否打开系统默认铃声
	 * @param vibrate
	 *            是否打开系统默认震动
	 * @param lights
	 *            是否打开系统默认闪光
	 * @return
	 */
	public NotifyUtil setCompatBuilder(String channelId, String channelName, String ticker, String title,
                                       String content, boolean sound, boolean vibrate, boolean lights) {
		return setCompatBuilder(channelId, channelName, R.mipmap.ic_launcher, ticker, title, content, sound, vibrate, lights);
	}

	/**
	 * 设置Builder
	 *
	 * @param channelId
	 * 			  通道ID
	 * @param channelName
	 * 			  通道名称
	 * @param smallIcon
	 *            默认小图标
	 * @param ticker
	 *            在顶部状态栏中的提示信息
	 * @param title
	 *            设置通知中心的标题
	 * @param content
	 *            设置通知中心中的内容
	 * @param sound
	 *            是否打开系统默认铃声
	 * @param vibrate
	 *            是否打开系统默认震动
	 * @param lights
	 *            是否打开系统默认闪光
	 * @return
	 */
	public NotifyUtil setCompatBuilder(String channelId, String channelName, int smallIcon, String ticker, String title, String content,
                                       boolean sound, boolean vibrate, boolean lights) {
		// 创建一个NotificationManager的引用
		notificationManager = (NotificationManager) mApplcation.getSystemService(Activity.NOTIFICATION_SERVICE);
		if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
			int importance = NotificationManager.IMPORTANCE_LOW;
			NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
			notificationChannel.enableVibration(true);
			notificationChannel.setShowBadge(false);
			notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
			if (notificationManager != null) {
				notificationManager.createNotificationChannel(notificationChannel);
			}
		}
        builder = new NotificationCompat.Builder(mApplcation);
        // 设置顶部状态栏的小图标
        builder.setSmallIcon(smallIcon);
		// 在顶部状态栏中的提示信息
        builder.setTicker(ticker);
		// 设置通知中心的标题
        builder.setContentTitle(title);
		// 设置通知中心中的内容
        builder.setContentText(content);
        builder.setWhen(System.currentTimeMillis());
		/*
		 * 将AutoCancel设为true后，当你点击通知栏的notification后，它会自动被取消消失,
		 * 不设置的话点击消息后也不清除，但可以滑动删除
		 */
        builder.setAutoCancel(true);
		/*
		 * 从Android4.1开始，可以通过以下方法，设置notification的优先级，
		 * 优先级越高的，通知排的越靠前，优先级低的，不会在手机最顶部的状态栏显示图标
		 */
        builder.setPriority(NotificationCompat.PRIORITY_MAX);
		/*
		 * Notification.DEFAULT_ALL：铃声、闪光、震动均系统默认。
		 * Notification.DEFAULT_SOUND：系统默认铃声。
		 * Notification.DEFAULT_VIBRATE：系统默认震动。
		 * Notification.DEFAULT_LIGHTS：系统默认闪光。
		 */
        builder.setDefaults(Notification.DEFAULT_ALL);
		return this;
	}

	/**
	 * 初始化Intent
	 *
	 * @param cls
	 * @return
	 */
	public NotifyUtil initIntent(Class cls) {
		this.mIntent = new Intent(mApplcation, cls);
		mInClass = mIntent.getClass();
		return this;
	}

	/**
	 * Intent 传入key value
	 *
	 * @param key
	 * @param value
	 * @return
	 */
	public NotifyUtil putIntent(String key, Object value) {
		ActivityUtils.putExtra(mInClass, mIntent, key, value);
		return this;
	}

	/**
	 * 设置PendingIntent
	 *
	 * @return
	 */
	public NotifyUtil setPendingIntent() {
		mIntent.getIntExtra(CommonConstant.FILENAMEMODEL, -1);
		PendingIntent pendingIntent = PendingIntent.getActivity(mApplcation,
				0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);
		return this;
	}


	/**
	 * 发送通知根据Id
	 *
	 * @param id
	 */
	public NotifyUtil send(int id) {
		Notification notification = getNotification();
		// 发送该通知
        notificationManager.notify(id, notification);
		return this;
	}

	/**
	 * 发送通知根据tag和Id
	 *
	 * @param tag
	 * @param id
	 * @return
	 */
	public NotifyUtil send(String tag, int id) {
		Notification notification = getNotification();
		// 发送该通知
		notificationManager.notify(tag, id, notification);
		return this;
	}

	/**
	 * 得到通知
	 *
	 * @return
	 */
	private Notification getNotification() {
		Notification notification = builder.build();
        notification.flags |= Notification.FLAG_ONGOING_EVENT;
        return notification;
	}

	/**
	 * 清除全部
	 *
	 * @return
	 */
	public NotifyUtil clearAll() {
        notificationManager.cancelAll();
		return this;
	}

	/**
	 * 根据Id清除
	 *
	 * @return
	 */
	public NotifyUtil clear(int id) {
        notificationManager.cancel(id);
		return this;
	}

	/**
	 * 根据Id和tag清除
	 *
	 * @return
	 */
	public NotifyUtil clear(String tag, int id) {
        notificationManager.cancel(tag, id);
		return this;
	}

}
